Chart for WPF and Silverlight > Chart Types > HighLowOpenClose Chart > Creating a Hi-Low-Open-Close Chart in Code |
To programmatically create a HiLowOpenClose chart, use the following code:
C# |
Copy Code
|
---|---|
HighLowOpenCloseSeries ds = new HighLowOpenCloseSeries() { XValueBinding = new System.Windows.Data.Binding("NumberOfDay"), HighValueBinding = new System.Windows.Data.Binding("High"), LowValueBinding = new System.Windows.Data.Binding("Low"), OpenValueBinding = new System.Windows.Data.Binding("Open"), CloseValueBinding = new System.Windows.Data.Binding("Close"), SymbolStrokeThickness = 1, SymbolSize = new Size(5, 5) } ds.PlotElementLoaded += (s, e) => { PlotElement pe = (PlotElement)s; double open = (double)pe.DataPoint["OpenValues"]; double close = (double)pe.DataPoint["CloseValues"]; if (open > close) { pe.Fill = green; pe.Stroke = green; } else { pe.Fill = red; pe.Stroke = red; } }; |
For example, if the values were provided by the application as collections, then you could use the code below to create the data series:
C# |
Copy Code
|
---|---|
// create data series HighLowOpenCloseSeries ds = new HighLowOpenCloseSeries(); ds.XValuesSource = dates; // dates are along x-axis ds.OpenValuesSource = open; ds.CloseValuesSource = close; ds.HighValuesSource = hi; ds.LowValuesSource = lo; // add series to chart chart.Data.Children.Add(ds); // set chart type chart.ChartType = isCandle ? ChartType.Candle : ChartType.HighLowOpenClose; |
Another option is to use data-binding. For example, if the data is available as a collection of StockQuote objects such as:
C# |
Copy Code
|
---|---|
public class Quote { public DateTime Date { get; set; } public double Open { get; set; } public double Close { get; set; } public double High { get; set; } public double Low { get; set; } } |
Then the code that creates the data series would be as follows:
C# |
Copy Code
|
---|---|
// create data series HighLowOpenCloseSeries ds = new HighLowOpenCloseSeries(); // bind all five values ds.XValueBinding = new Binding("Date"); // dates are along x-axis ds.OpenValueBinding = new Binding("Open"); ds.CloseValueBinding = new Binding("Close"); ds.HighValueBinding = new Binding("High"); ds.LowValueBinding = new Binding("Low"); // add series to chart chart.Data.Children.Add(ds); // set chart type chart.ChartType = isCandle ? ChartType.Candle : ChartType.HighLowOpenClose; |